<?php
/**
 * File to handle all API requests
 * Accepts GET and POST
 *
 * Each request will be identified by TAG
 * Response will be JSON data
 
  /**
 * check for POST request
 */
mysql_connect("localhost", "root", "root") or die(mysql_error());
mysql_select_db("test") or die(mysql_error());
if (isset($_POST['tag'])) {
    // get tag
    $tag = $_POST['tag'];
 
    // include db handler
    //require_once 'include/DB_Functions.php';
    //$db = new DB_Functions();
 
    // response Array
    $response = array("tag" => $tag, "success" => 0, "error" => 0);
 
    // check for tag type
    if ($tag == 'show') {
	// To be implemented
        // Request type is Register new route
        $uid = $_POST['uid'];
        
 
        // check if route already exists
	$res = mysql_query("SELECT * from user_node WHERE user_id = '$uid'");
	$rt = mysql_fetch_array($res);
	$nid=$rt["node_id"];
        $no_of_rows = mysql_num_rows($res);
        if ($no_of_rows > 0) {
            // user is already existed - error response
            $result = mysql_query("SELECT * FROM node WHERE node_id = $nid");
            // return user details
            $route = mysql_fetch_array($result);
	    if ($route != false) {
                // user stored successfully
                $response["success"] = 1;
                $response["uid"] = $route["node_id"];
                $response["route"]["src_name"] = $route["src_name"];
                $response["route"]["des_name"] = $route["dest_name"];
                $response["route"]["via_name"] = $route["via_name"];
                echo json_encode($response);
            } else {
                // user failed to store
                $response["error"] = 1;
                $response["error_msg"] = "No Routes";
                echo json_encode($response);
            }
        } else {
           $response["error"] = 1;
                $response["error_msg"] = "Error occured !!";
                echo json_encode($response);
        }
    } else {
        echo "Invalid Request";
    }
} else {
    echo "Access Denied";
}
?>
